package com.xiaolanshu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.tools.corba.se.idl.constExpr.Or;
import com.xiaolanshu.entity.Order;
import com.xiaolanshu.entity.ShoppingCart;
import com.xiaolanshu.mapper.OrderMapper;
import com.xiaolanshu.service.OrderService;
import com.xiaolanshu.vo.TableDataVo;
import com.xiaolanshu.vo.TableVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.*;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    //将购物车商品信息添加到订单
    @Override
    public void addShoppingCartGoods(ShoppingCart shoppingCart,Double returnAmount,Double costs) {
        //创建Order对象接受购物车商品信息
        Order order = new Order();
        order.setUserId(shoppingCart.getUserId());
        order.setGoodsId(shoppingCart.getGoodsId());
        order.setGoodsName(shoppingCart.getGoodsName());
        order.setPicture(shoppingCart.getPicture());
        order.setCount(shoppingCart.getCount());
        order.setAmount(shoppingCart.getAmount());
        order.setReturnAmount(returnAmount);
        order.setPrice(shoppingCart.getPrice());
        order.setCost(costs);
        //将商品信息保存到订单
        orderMapper.insert(order);

    }
    //根据用户id查询订单列表
    @Override
    public List<Order> OrderListByUserzId(Long userId) {

        LambdaQueryWrapper<Order> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Order::getUserId,userId).eq(Order::getDeleted,0);
        return orderMapper.selectList(lqw);
    }
    //商品直接结算
    @Override
    public void goodsSettle(Order order) {
        //保存订单信息
        orderMapper.insert(order);

    }
    //根据日期查询订单
    @Override
    public List<Order> findOrder(Map param) {
        String year = (String) param.get("year");
        String month = (String) param.get("month");
        String day = (String) param.get("day");
        //创建一个date字符串拼接如期进行模糊查询，年份不能为空所以直接拼接
        String date = year;
        //如果月份不为空拼接月份
        if(month != null){
            date += "-"+month;
        }
        //如果日份不为空，拼接月份
        if(day != null){
            date += "-"+day;
        }
        //构造查询条件，根据date模糊查询订单
        LambdaQueryWrapper<Order> lqw = new LambdaQueryWrapper<>();
        lqw.like(Order::getCreated,date).eq(Order::getDeleted,0);
        return orderMapper.selectList(lqw);

    }
    //获取返利报表数据
    @Override
    public TableVo ReturnChart(String year) {

        //当前年份和月份
        Calendar c = Calendar.getInstance();
        String yearNow = String.valueOf(c.get(Calendar.YEAR));
        int monthNow = c.get(Calendar.MONTH)+1;

        String[] monthArr = new String[monthNow];

        //如果年份为当前年份,只显示到当前月份
        if (year.equals(yearNow)){
            for (int i = 0; i < monthNow; i++) {
                String month = null;
                if(i<10){
                   month = "0" + String.valueOf(i+1);
                }
                monthArr[i] = month;
            }
        }
        else {
            monthArr = new String[]{"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};
        }

        //将每月的销售金额和返利金额分别保存在数组里
        Double[] amountArr = new Double[monthArr.length];
        Double[] returnAmountArr = new Double[monthArr.length];
        Double[] returnRateArr = new Double[monthArr.length];
        //遍历月份，根据月份和年份查询订单的返利金额和销售金额
        for (String month : monthArr
        ) {

            //拼接年份和月份
            String date = year +"-"+ month;
            //构造查询条件
            LambdaQueryWrapper<Order> lqw = new LambdaQueryWrapper<>();
            lqw.like(Order::getCreated, date).eq(Order::getDeleted, 0).select(Order::getAmount, Order::getReturnAmount);
            List<Order> orders = orderMapper.selectList(lqw);

            //遍历orders统计每月的销售金额和返利金额和返利率
            Double amount = 0D;
            Double returnAmount = 0D;
            Double returnRate = 0D;

            for (Order order : orders
            ) {
                amount += order.getAmount();
                returnAmount += order.getReturnAmount();
                returnRate = returnAmount/amount;
            }

            int i = Integer.parseInt(month) - 1;

            DecimalFormat df=new DecimalFormat("0.00");
            amountArr[i] = Double.valueOf(df.format(amount));
            returnAmountArr[i] = Double.valueOf(df.format(returnAmount));
            returnRateArr[i] = Double.valueOf(df.format(returnRate));

        }
        List<TableDataVo> tableDataVoList = new ArrayList<>();

        TableDataVo tableDataVo1 = new TableDataVo();
        TableDataVo tableDataVo2 = new TableDataVo();
        TableDataVo tableDataVo3 = new TableDataVo();

        tableDataVo1.setName("销售金额");
        tableDataVo1.setData(amountArr);

        tableDataVo2.setName("返利金额");
        tableDataVo2.setData(returnAmountArr);

        tableDataVo3.setName("返利率");
        tableDataVo3.setData(returnRateArr);

        tableDataVoList.add(tableDataVo1);
        tableDataVoList.add(tableDataVo2);
        tableDataVoList.add(tableDataVo3);

        //查询12月销售额中最大的销售金额
        Double maxAmount = 0D;
        for (int i = 0; i < amountArr.length; i++) {
            if (amountArr[i] >maxAmount)
                maxAmount = amountArr[i];
        }
        //计算y轴刻度
        int scaleMax =  ((int)(maxAmount / 500.0) + 1);

        int[] yLeft = new int[scaleMax];

        for (int i = 0; i < scaleMax; i++) {
            yLeft[i] = (i+1)*500;
        }

        double[] yRight = {0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0};
        String[] xAxis = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};

        TableVo tableVo = new TableVo();
        tableVo.setXAxis(xAxis);
        tableVo.setYAxisLeft(yLeft);
        tableVo.setYAxisRight(yRight);
        tableVo.setTableDataVos(tableDataVoList);

        return tableVo;
    }

}
